package com.jhgsys.internal.peccancy.service.impl;

import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.peccancy.entity.PeccancyAuditRecord;
import com.jhgsys.internal.peccancy.mapper.PeccancyAuditRecordMapper;
import com.jhgsys.internal.peccancy.service.IPeccancyAuditRecordService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 违章审核记录 Service实现
 *
 * @author Wanghw
 * @date 2024-08-14 10:38:38
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PeccancyAuditRecordServiceImpl extends ServiceImpl<PeccancyAuditRecordMapper, PeccancyAuditRecord> implements IPeccancyAuditRecordService {

    /**
     * 查询（分页）
     * @author Wanghw
     * @date 2024-08-14 10:38:38
     * @param request QueryRequest
     * @param peccancyAuditRecord peccancyAuditRecord
     * @return IPage<PeccancyAuditRecord>
    */
    @Override
    public IPage<PeccancyAuditRecord> findPeccancyAuditRecordPage(QueryRequest request, PeccancyAuditRecord peccancyAuditRecord) {
        LambdaQueryWrapper<PeccancyAuditRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PeccancyAuditRecord::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<PeccancyAuditRecord> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author Wanghw
     * @date 2024-08-14 10:38:38
     * @param peccancyAuditRecord peccancyAuditRecord
     * @return List<PeccancyAuditRecord>
     */
    @Override
    public List<PeccancyAuditRecord> findPeccancyAuditRecordList(PeccancyAuditRecord peccancyAuditRecord) {
	    LambdaQueryWrapper<PeccancyAuditRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PeccancyAuditRecord::getDeletedFlg, FebsConstant.VALID);
		if(StringUtils.isNotBlank(peccancyAuditRecord.getInformationId())){
            queryWrapper.eq(PeccancyAuditRecord::getInformationId, peccancyAuditRecord.getInformationId());
        }
        if(StringUtils.isNotBlank(peccancyAuditRecord.getTenantId())){
            queryWrapper.eq(PeccancyAuditRecord::getTenantId, peccancyAuditRecord.getTenantId());
        }
        if(StringUtils.isNotBlank(peccancyAuditRecord.getAuditBusiness())){
            queryWrapper.eq(PeccancyAuditRecord::getAuditBusiness, peccancyAuditRecord.getAuditBusiness());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author Wanghw
    * @date 2024-08-14 10:38:38
    * @param id
    * @return PeccancyAuditRecord
    */
    @Override
    public PeccancyAuditRecord findPeccancyAuditRecordById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author Wanghw
     * @date 2024-08-14 10:38:38
     * @param peccancyAuditRecord peccancyAuditRecord
     */
    @Override
    @Transactional
    public void createPeccancyAuditRecord(PeccancyAuditRecord peccancyAuditRecord) {
        peccancyAuditRecord.setCreatedTime(new Date());
        this.save(peccancyAuditRecord);
    }

    /**
     * 修改违章审核记录
     * @author Wanghw
     * @date 2024-08-14 10:38:38
     * @param peccancyAuditRecord peccancyAuditRecord
     */
    @Override
    @Transactional
    public void updatePeccancyAuditRecord(PeccancyAuditRecord peccancyAuditRecord) {
        peccancyAuditRecord.setUpdatedTime(new Date());
        this.saveOrUpdate(peccancyAuditRecord);

    }

    /**
     * 逻辑删除
     * @author Wanghw
     * @date 2024-08-14 10:38:38
     * @param peccancyAuditRecord peccancyAuditRecord
     * @param peccancyAuditRecordIds peccancyAuditRecordIds
     */
    @Override
    @Transactional
    public void deletePeccancyAuditRecord(PeccancyAuditRecord peccancyAuditRecord,String peccancyAuditRecordIds) {
        List<String> list = Arrays.asList(peccancyAuditRecordIds.split(StringPool.COMMA));
        this.baseMapper.update(peccancyAuditRecord,new LambdaQueryWrapper<PeccancyAuditRecord>().in(PeccancyAuditRecord::getAuditRecordId,list));
	}

    /**
    * 物理删除
    * @author Wanghw
    * @date 2024-08-14 10:38:38
    * @param peccancyAuditRecordIds peccancyAuditRecordIds
    */
    @Override
    @Transactional
    public void physicallyDeletePeccancyAuditRecord(String peccancyAuditRecordIds) {
        List<String> list = Arrays.asList(peccancyAuditRecordIds.split(StringPool.COMMA));
        LambdaQueryWrapper<PeccancyAuditRecord> wapper = new LambdaQueryWrapper<>();
        wapper.in(PeccancyAuditRecord::getAuditRecordId,list);
        this.remove(wapper);
    }
}
